﻿using Prism.Regions;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;

namespace PrismDemo.Commons.Util
{
    //【1】继承 RegionAdapterBase<T>
    public class StackPanelRegionAdapter : RegionAdapterBase<StackPanel>
    {   //【2】创建构造函数
        public StackPanelRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) : base(regionBehaviorFactory)
        {
        }
        //【3】重写创建适配器方法
        protected override void Adapt(IRegion region, StackPanel regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement element in e.NewItems)
                    {
                        regionTarget.Children.Add(element);
                    }
                }
                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (FrameworkElement element in e.OldItems)
                    {
                        regionTarget.Children.Remove(element);
                    }
                }
                //handle other case
            };
        }
        //【4】重写生成区域方法
        protected override IRegion CreateRegion() => new Region();
    }
}

